var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "path", "crypto", "../../../com/configTool", "../Util/readFromNet", "../Util/initDB", "../Util/saveToFile", "../Util/readFromDisk", "../../../img/imgMD5", "../../TileTool/TileUtilBing", "../Util/readFromDB", "../Util/saveToDB"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const path_1 = __importDefault(require("path"));
    const crypto_1 = __importDefault(require("crypto"));
    const configTool_1 = __importDefault(require("../../../com/configTool"));
    const readFromNet_1 = __importDefault(require("../Util/readFromNet"));
    const initDB_1 = __importDefault(require("../Util/initDB"));
    const saveToFile_1 = __importDefault(require("../Util/saveToFile"));
    const readFromDisk_1 = __importDefault(require("../Util/readFromDisk"));
    const imgMD5_1 = __importDefault(require("../../../img/imgMD5"));
    const TileUtilBing_1 = __importDefault(require("../../TileTool/TileUtilBing"));
    const readFromDB_1 = __importDefault(require("../Util/readFromDB"));
    const saveToDB_1 = __importDefault(require("../Util/saveToDB"));
    let Loading = true;
    const DBName = 'BingMaps';
    const BingMapUrl = 'https://ecn.t1.tiles.virtualearth.net/tiles/a{tileXYToQuadKey}.jpeg?g=100';
    const tileUtilBing = new TileUtilBing_1.default();
    let BingTile = {
        async init() {
            if (Loading) {
                Loading = false;
                await configTool_1.default.init();
                configTool_1.default.preservableONDB() && await (0, initDB_1.default)(DBName);
            }
        },
        async getTileFileBufferByXYZ(x, y, zoom) {
            const tileXYToQuadKey = tileUtilBing.tileXYToQuadKey(x, y, zoom);
            return await this.getTileFileBuffer(tileXYToQuadKey);
        },
        async getTileFileBuffer(tileXYToQuadKey) {
            await this.init();
            let err, buffer, dataSource;
            const url = BingMapUrl.replace('{tileXYToQuadKey}', tileXYToQuadKey.toString());
            const { x, y, zoom } = tileUtilBing.quadKeyToTileXY(tileXYToQuadKey);
            if (zoom < 0 || zoom > 22) {
            }
            const relativePath = `\\${zoom + 1}\\${x}\\${y}.jpeg`;
            const fileSavePath = path_1.default.join(configTool_1.default.appBasePath, `temp/fileOut/${DBName}`, relativePath);
            [err, buffer, dataSource] = await (0, readFromDisk_1.default)({ fileSavePath, err, buffer, dataSource });
            [err, buffer, dataSource] = await (0, readFromDB_1.default)({ DBName, relativePath, err, buffer, dataSource });
            [err, buffer, dataSource] = await (0, readFromNet_1.default)({
                url, headers: {}, err, buffer, dataSource
            });
            if (buffer) {
                const md5Str = crypto_1.default.createHash('md5').update(buffer).digest('hex');
                if (md5Str !== imgMD5_1.default.emptyPng && md5Str !== imgMD5_1.default.transparentPng) {
                    dataSource !== 'DB' && configTool_1.default.preservableONDB() && (0, saveToDB_1.default)({ DBName, relativePath, buffer }).then();
                    dataSource === 'Net' && configTool_1.default.preservableONFile() && (0, saveToFile_1.default)(fileSavePath, buffer).then();
                }
            }
            return [err, buffer, dataSource];
        }
    };
    exports.default = BingTile;
});
